package com.buddy.design.pattern._03proxy.cglib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class MeiPo implements MethodInterceptor {

    public Object getInstance(Object obj){
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(obj.getClass());
        enhancer.setCallback(this);
        return enhancer.create();
    }

    /*
     *CGLIB
     * 同样是字节码重组
     * 对使用API的用户来说是无感知的
     */
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println(">>>>执行方法前...");
        methodProxy.invokeSuper(o,objects);
        System.out.println(">>>执行方法后!");
        return  null;
    }
}
